<template>
  <div>
    <h1>工程管理</h1>
    <br>
    <el-row>
      <el-col :span="14">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="工程名称">
            <el-input v-model="formInline.name" placeholder="工程名称（模糊查询）"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>

      </el-col>
      <el-col :span="10">
        <el-button type="warning" @click="addDeptEdit" style="float:right;margin-left: 30px;">新增工程
        </el-button>
      </el-col>

      <el-col :span="24">
        <el-table
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%">
          <el-table-column
              prop="name"
              label="工程名称">
          </el-table-column>
          <el-table-column
              prop="leader"
              label="负责人">
          </el-table-column>
          <el-table-column
              prop="phone"
              label="联系电话">
          </el-table-column>
          <el-table-column
              prop="email"
              label="邮箱">
          </el-table-column>
          <el-table-column
              prop="status"
              label="状态">
            <template v-slot="scope">
              <el-switch
                  @change="setStatus(scope.row)"
                  active-value="0"
                  inactive-value="1"
                  v-model="scope.row.status"
                  active-color="#13ce66"
                  inactive-color=rgb(100,105,176)>
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
              prop="province"
              label="省会">
          </el-table-column>
          <el-table-column
              prop="updateTime"
              label="更新时间">
          </el-table-column>
          <el-table-column
              fixed="right"
              label="操作"
              width="150px">
            <template v-slot="scope">
              <el-button @click="updateDeptBtn(scope.row.id)" size="small">修改</el-button>
              <el-button @click="deleteDeptBtn(scope.row.id)" type="danger" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>

      <el-col :span="24" style="margin-top: 20px;text-align: center">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 30, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="工程编辑" :visible.sync="dialogFormVisible">
      <AddDept v-if="dialogFormVisible" :id="editId" @close-dialog="closeDialog">
      </AddDept>
    </el-dialog>
  </div>

</template>

<script>
import request from "@/utils/request";
import AddDept from "@/components/pages/AddDept";

export default {
  name: "DeptPage",
  components: {AddDept},
  data() {
    return {
      formInline: {
        name: '',
      },
      editId: 0,
      dialogFormVisible: false,
      // 用户数据
      tableData: [],
      // 模糊搜索
      input: "",
      // 分页的 pageSize total currentPage
      pageSize: 5,
      total: 0,
      currentPage: 1,
    }
  },
  created() {
    this.loadDept()
  },
  methods: {
    async loadDept() {
      let data = await request("dept?page=" + this.currentPage + "&size=" + this.pageSize + "&name=" + this.formInline.name + "&orderBy=update_time desc");
      this.tableData = data.list
      this.total = data.total
    },
    // 新增部门按钮
    addDeptEdit() {
      this.editId = 0
      this.dialogFormVisible = true;
    },
    updateDeptBtn(id) {
      this.dialogFormVisible = true
      this.editId = id;
    },
    // 分页
    handleSizeChange(i) {
      this.pageSize = i;
      this.currentPage = 1;
      this.loadDept()
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.loadDept()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 删除部门按钮
    deleteDeptBtn(id) {
      this.$confirm('此操作将永久禁用该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request(`dept?id=${id}`, "DELETE").then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.loadDept()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    closeDialog(b) {
      this.dialogFormVisible = false;
      if (b) {
        this.loadDept()
      }
    },
    onSubmit() {
      this.loadDept()
    },
    // 状态
    setStatus({id, status}) {
      request("dept", "PUT", {id: id, status: status}).then(() => {
        this.$message.success("修改状态成功")
      }).catch(() => {
        this.$message.error("修改状态失败")
      })
    },
  }
}
</script>

<style scoped>

</style>